package com.qf.zyh.web.controller;

import com.qf.zyh.common.Const;
import com.qf.zyh.common.ResponseResult;
import com.qf.zyh.common.ResultType;
import com.qf.zyh.entity.AdminUser;
import com.qf.zyh.entity.Menu;
import com.qf.zyh.services.MenuServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/menu")
public class MenuController {
    @Autowired
    private MenuServices services;

    @RequestMapping("/getMenu")
    public ResponseResult getMenu(HttpSession session){
        AdminUser au = (AdminUser) session.getAttribute(Const.IS_LOGIN);
        if (au==null) {
            return ResponseResult.getFail("请先登录");
        }
        List<Menu> list =services.getMenuByUid(au.getId());
        return ResponseResult.getSuccess(list);
    }
    @RequestMapping("/list")
    public ResultType getlist(Integer page, Integer limit){
        return services.getlist(page,limit);
    }
    @RequestMapping("/delete")
    public ResponseResult delete(Integer[] ids){
        return services.delete(ids);
    }

    @RequestMapping("/add")
    public ResponseResult add(Menu m){
        try {
            return services.addMenu(m);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseResult.getFail("新增错误");
    }
    @RequestMapping("/update")
    public ResponseResult update(Menu m){
        try {
            return services.update(m);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseResult.getFail("修改错误");
    }
    @RequestMapping("/info")
    public Menu info(Integer id){
        return services.info(id);
    }
    @RequestMapping("/tree")
    public List<Menu> tree(){
        return services.tree();
    }

}
